package cn.wgx.module.mind.websocket.util;

import cn.wgx.common.util.SpringContextHolder;
import lombok.Data;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 房间状态工具
 * isForbidden : 全体禁言
 */
public class RoomStatusTool {

    private static RedisTemplate redisTemplate;

    private static final String CHAT_ROOM_ALL_FORBIDDEN_KEY = "CHAT_ROOM_FORBIDDEN_KEY_";

    static {
        redisTemplate = SpringContextHolder.getBean("redisTemplate");
    }


    public static boolean isAllForbidden(String roomId){
        RoomStatus roomstatus = getRoomstatus(roomId);
        return roomstatus.isForbidden();
    }

    public static void allForbidden(String roomId){
        RoomStatus roomstatus = getRoomstatus(roomId);
        roomstatus.setForbidden(true);
        saveRoomstatus(roomId, roomstatus);
    }

    public static void unAllforbidden(String roomId){
        RoomStatus roomstatus = getRoomstatus(roomId);
        roomstatus.setForbidden(false);
        saveRoomstatus(roomId, roomstatus);
    }

    private static RoomStatus getRoomstatus(String roomId){
        Object o = getHash().get(CHAT_ROOM_ALL_FORBIDDEN_KEY , roomId);
        if(o instanceof RoomStatus){
            return (RoomStatus)o;
        }else{
            return new RoomStatusTool.RoomStatus();
        }
    }

    private static void saveRoomstatus(String roomId, RoomStatus roomStatus){
        getHash().put(CHAT_ROOM_ALL_FORBIDDEN_KEY, roomId, roomStatus);
    }

    private static HashOperations getHash(){
        return redisTemplate.opsForHash();
    }

    @Data
    public static class RoomStatus {

        private boolean isForbidden = false;

    }

}
